using System.Collections;
using System.Collections.Generic;

namespace BoxBinary.AspectCache.Core.CacheManagement
{
    /// <summary>
    /// A custom comparer class used to sort instances of <see cref="System.Collections.Generic.KeyValuePair<string, string>"/>
    /// </summary>
    public class CacheKeyUniqueParamsComparer : IComparer<KeyValuePair<string, string>>
    {
        #region IComparer<KeyValuePair<string,string>> Members

        /// <summary>
        /// Compares two instances of <see cref="System.Collections.Generic.KeyValuePair<string, string>"/>
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns>An integer value ascertaining whether x should be sorted higher, the same, or lower than y. A value of 0 indicates the two objects are
        /// functionally the same for the purposes of a sort comparison.</returns>
        public int Compare(KeyValuePair<string, string> x, KeyValuePair<string, string> y)
        {
            // Sort on the key and then the value
            int keyComparison = new CaseInsensitiveComparer().Compare(x.Key, y.Key);
            if (keyComparison == 0)
            {
                // They are the same so check the value
                return new CaseInsensitiveComparer().Compare(x.Value, y.Value);
            }
            else
                return keyComparison;
        }

        #endregion
    }
}